<template>
  <div id="logIn">
    <!-- 登录 -->
    <div class="log_content">
      <div class="log_bg">
        <iframe src="canvas.html" />
      </div>
      <div class="log_conBlock">
        <div class="log_logo">
          <img src="../assets/logo.png">
        </div>
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username">
            <el-input placeholder="请输入您的用户名" v-model="loginForm.username">
              <i slot="prefix" class="el-input__icon el-icon-user"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入您的密码" v-model="loginForm.password" @keyup.enter.native="login">
              <i slot="prefix" class="el-input__icon el-icon-lock"></i>
            </el-input>
          </el-form-item>
          <el-form-item class="bu_block">
            <el-button type="primary" @click="login">登 &nbsp;陆</el-button>
            <el-button type="primary" @click="reset">重 &nbsp;置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: { // 登陆表单数据
        username: 'admin',
        password: ''
      },
      loginRules: { // 表单预验证
        username: [
          { required: true, message: '用户名是必填的', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码是必填的', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () { // 登陆
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) { return } // 如果不通过预验证则直接跳出
        // axios数据，使用async await处理异步，使用解构赋值取出响应头数据
        const { data: res } = await this.$http.post('login', this.loginForm)
        // 登录不成功则跳出并弹出错误提示
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        // 登录成功后提示
        this.$message.success(res.meta.msg)
        // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
        // 1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
        // 1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
        window.sessionStorage.setItem('token', res.data.token)
        // 编程式导航跳转后台主页
        this.$router.push('/home')
      })
    },
    reset () { // 重置登陆表单
      this.$refs.loginRef.resetFields()
    }
  }
}

</script>
<style scoped>
  html,body,#logIn {height:100%;}
  .log_content {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    z-index: 999;
  }

  .log_bg {
    position: fixed;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }

  .log_bg iframe {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 1920px;
    height: 1920px;
    margin-left: -960px;
    margin-top: -960px;
    border: 0px;
  }

  .log_conBlock {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 400px;
    margin-left: -200px;
    margin-top: -100px;
    background: #fff;
    padding: 30px 20px 10px 20px;
    min-height: 186px;
  }

  .log_logo {
    position: absolute;
    top: -60px;
    text-align: center;
    width: 100%;
    left: 0px;
  }

  .log_logo img {
    display: block;
    margin: auto;
  }

  .bu_block {
    text-align: center;
  }
  .bu_block button {min-width:80px}

</style>
